package controllers

import (
	"baokaobang/utils"
	"fmt"
	"strconv"
	"time"

	uuid "github.com/iris-contrib/go.uuid"
	"github.com/kataras/iris/v12"
)

type Pay struct {
	Ctx iris.Context
}

//付款 goods 0:报考咨询师方案 1:招办专家一对一 2:招办专家面对面
func (c *Pay) PostPay() interface{} {
	channel := c.Ctx.PostValue("channel")
	price := c.Ctx.PostValue("price")
	goods := c.Ctx.PostValue("goods")
	payType := c.Ctx.PostValue("payType")

	if channel == "" || price == "" || goods == "" || payType == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	uuidStr, err := uuid.NewV4()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("生成uuid失败: %s", err), "")
	}

	times := time.Now().Unix()
	timeStr := strconv.FormatInt(times, 10)

	//生成订单号
	orderID := timeStr + uuidStr.String()

	//组织数据
	var data map[string][]string

	data["paytype"] = []string{payType}
	data["randomstr"] = []string{orderID}

	_, err = utils.TongLianPay(data)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("支付失败: %s", err), "")
	}

	return utils.CommonRes(20000, fmt.Sprintf("支付成功"), "")
}
